import pygame

from source.stats import Stats
from source.image import Image

class Hero:
    # 接受了两个参数，一个是屏幕矩形，另一个是游戏状态
    def __init__(self, screen_rect, stats):

        self.screen_rect = screen_rect
        self.stats = stats

        self.images = Image.heros
        self.image = self.images[0]
        # self.mask是在碰撞检测时使用的，利用它可以完美的检测到物体之间的碰撞
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.reset()

    def reset(self):
        self.rect.midbottom = self.screen_rect.midbottom
        # 表示当前图片
        self.image_index = 0
        # 用来标记是否和敌机发生碰撞
        self.is_collide = False

    # 用来管理和向屏幕绘制飞机图片，每一帧都会被调用一次
    def draw(self, display_surface):
        if self.is_collide:
            if self.image_index < len(self.images) - 1:
                self.image_index += 1
            else:
                pygame.time.delay(200)
                self.stats.state = Stats.GAMEOVER
        else:
            self.image_index = not self.image_index

        self.image = self.images[self.image_index]

        display_surface.blit(self.image, self.rect)

    # def update(self, pos):
    #     if self.rect.collidepoint(pos):
    #         self.rect.center = pos
